package com.example.gyj.controller;

import com.example.gyj.entity.OrderInfo;
import com.example.gyj.entity.User;
import com.example.gyj.result.CodeMsg;
import com.example.gyj.result.Result;
import com.example.gyj.service.GoodsService;
import com.example.gyj.service.OrderService;
import com.example.gyj.service.RedisService;
import com.example.gyj.service.UserService;
import com.example.gyj.vo.GoodsVo;
import com.example.gyj.vo.OrderDetailVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Gao on 2018/10/31.
 */
@Controller
@RequestMapping(value = "/order")
public class OrderController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 获取订单详情页
     * @param user
     * @param orderId
     */
    @GetMapping(value = "/detail")
    @ResponseBody
    public Result<OrderDetailVo> info(User user, @RequestParam("orderId") long orderId) {
        if (user == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        OrderInfo order = orderService.getOrderById(orderId);
        if (order == null) {
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        }
        long goodsId = order.getGoodsId();
        GoodsVo goodsVo = goodsService.getGoodsVoByGoodsId(goodsId);
        OrderDetailVo vo = new OrderDetailVo();
        vo.setOrder(order);
        vo.setGoodsVo(goodsVo);
        return Result.success(vo);
    }
}
